package com.dora.basic.juc.blockqueue;

/**
 * 借助阻塞队列 实现生产者消费者模型
 * */
public class Test {
    public static void main(String[] args) {
        MyBlockingQueue queue = new MyBlockingQueue();

        //生产者模型
        Thread t1 = new Thread(() -> {
            int num = 1;
            while (true) {
                try {
                    queue.put(String.valueOf(num));
                    System.out.println("生产者生产" + num);
                    num++;
                    //生产者有节奏生产
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        //消费者模型
        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    int tmp = Integer.parseInt(queue.take());
                    System.out.println("消费者消费" + tmp);
                    //消费者有节奏消费
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
    }
}